﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseLinQ.DataContex;
using Core;
using Module;

namespace _12CSolution.Cpanel.Menu
{
    public partial class MenuLeft : System.Web.UI.UserControl
    {
        MenuLogic logicM = new MenuLogic();
        string _connectString = ConfigurationSettings.AppSettings["ConnectString"];
        //List<Modul> lstModul = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            string url = Request.RawUrl.ToString();
            string _iMenuChaID = "1";
            if (!IsPostBack)
            {
                string strLeftMenu = "";
                strLeftMenu = GenMenuLeft(Utils.GetUserId().ToString(), Utils.GetSession_value(Response, Page, Constants.session_vaitroquanly_id), Utils.GetSession_value(Response, Page, Constants.session_donviquanly_id));
                strLeftMenu = Utils.setHoverMenu(strLeftMenu, url, " class='leftMenuActive'");
                Session[_iMenuChaID] = strLeftMenu;
                ltrLeftMenu.Text = strLeftMenu + "</ul></div>";
            }
        }
        public string GenMenuLeft(string _NguoiDungID, string _iVaiTroID, string _iDonViID)
        {
            MenuLogic logicM = new MenuLogic();
            string _html = "";
            string check = "";
            string control = "";
            string thumuc = "";
            string _url = "";
            List<tbl_Menu> menu = logicM.GetMenuChild();
            List<tbl_Menu> lstMenu = new List<tbl_Menu>();
            if (_NguoiDungID != "" && _iVaiTroID != "")
            {
                lstMenu = logicM.FilterValidMenu(_NguoiDungID, _iVaiTroID, _iDonViID, menu);
            }
            //foreach (tbl_Menu itemM in lstMenu)
            for (int i = 0; i < lstMenu.Count(); i++)
            {
                control = "";
                if (lstMenu[i].sTenControl != null && lstMenu[i].sTenControl != "")
                {
                    control = lstMenu[i].sTenControl.Replace(".ascx", "") + "/";
                }
                if (lstMenu[i].sTenThuMuc != null && lstMenu[i].sTenThuMuc != "")
                {
                    thumuc = lstMenu[i].sTenThuMuc;
                }
                _url = ResolveUrl("~/") + thumuc + "/" + control + "Default.aspx";
                if (control == "")
                {
                    _url = "#";
                }
                if (lstMenu[i].iCap == 1)
                {
                    _html += "<a href='" + _url + "' class='menuitem'>" + lstMenu[i].sTenMenu + "</a>";
                }
                else
                {
                    if (lstMenu[i].fk_iMenuChaID.ToString() != check)
                    {
                        tbl_Menu mncha = logicM.GetMenuCha(lstMenu[i].pk_iMenuID.ToString());
                        _html += "<a class='menuitem submenuheader' href='#'>" + mncha.sTenMenu + "</a>";
                        _html += "<div class='submenu'>";
                        _html += "<ul><li><a href='" + _url + "'>" + lstMenu[i].sTenMenu + "</a></li>";

                        check = lstMenu[i].fk_iMenuChaID.ToString();
                    }
                    else
                    {
                        _html += "<li><a href='" + _url + "'>" + lstMenu[i].sTenMenu + "</a></li>";
                    }

                    if (i < (lstMenu.Count() - 1) && lstMenu[i + 1].fk_iMenuChaID.ToString() != check)
                    {
                        _html += "</ul></div>";
                    }
                }
            }
            return _html;
        }
    }
}